
#include "LCD_TFT_DRIVER.hpp"
char LCD_TFT::toprintf[line_number][char_per_line];
u8 LCD_TFT::last_line_number = 8; //定义行数总数
char data_temp[char_per_line];    //设定暂时存储的数字
//清空所有内容
void LCD_TFT::clear_buffer(void)
{
    int i = 0;
    int j = 0;
    for (i = 0; i < line_number; i++)
    {
        for (j = 0; j < char_per_line; j++)
        {
            LCD_TFT::toprintf[i][j] = 0;
        }
    }
    for (j = 0; j < char_per_line; j++)
    {
        data_temp[j] = 0;
    }
}
u8 LCD_TFT::init(void)
{
    tft180_init();
    tft180_clear(0x0fff);
    LCD_TFT::replace_text(0, "lcd is inited 16Bytes", 1);
    // tft180_show_string(0, 0, );
    LCD_TFT::clear_buffer();
    LCD_TFT::add_text("lcd is ready", 1);
    return 0;
}
//在空位处添加自动的文字
void LCD_TFT::add_text(const char text[], bool is_refresh)
{
    int i = 1;
    while (i <= line_number) //查询空白数量
    {
        //如果没有空白就加在最后一行
        if (LCD_TFT::toprintf[i][0] == 0 || i == last_line_number)
        {
            int j = 0;
            while (j < char_per_line && text[j] != '\0')
            {
                LCD_TFT::toprintf[i][j] = text[j];
                j++;
            }
            LCD_TFT::toprintf[i][j] = '\0';
            break;
        }
        i++;
    }
    if (is_refresh)
    {
        show_text_refresh(i);
    }
}
//清除该行所有的文字
void LCD_TFT::remove_text(u8 location, bool is_refresh)
{
    if (location < 10 || location >= 0)
    {
        int j = 0;
        while (j < char_per_line)
        {
            LCD_TFT::toprintf[location][j] = 0;
            j++;
        }
    }
    if (is_refresh)
    {
        LCD_TFT::show_text_refresh(location);
    }
}
//替换某行整数
void LCD_TFT::replace_int(u8 location, int value, int place, bool is_refresh)
{
    int i = char_per_line - 1;
    if (place < char_per_line)
    {
        while (i && place-- && value)
        {
            data_temp[i] = (value % 10) + '0';
            value /= 10;
            i--;
        }
        while (i)
        {
            data_temp[i] = ' ';
            i--;
        } //把其余地方显示空格
        LCD_TFT::replace_text(location, data_temp, is_refresh);
        while (i < char_per_line)
        {
            data_temp[i] = ' '; //字符清零
            i++;
        }
    }
}
//替换某行小数
void LCD_TFT::replace_float(u8 location, double value, u8 integer_num, u8 point_num, bool is_refresh)
{
    int i = char_per_line - 1;
    int integer_part = (int)value;
    double point_part = value - integer_part;
    int point_part_integer = 0;
    if (integer_num + point_num < char_per_line - 1)
    {
        // printf("%lf\n", point_part);
        point_part_integer = (int)(point_part * pow(10, point_num));
        while (point_num--) //确保小数点位数正确，先判断再自减
        {
            data_temp[i] = point_part_integer % 10 + '0';
            point_part_integer /= 10;
            i--;
        }
        data_temp[i] = '.';
        i--;
        while (i && integer_num-- && integer_part)
        {
            data_temp[i] = integer_part % 10 + '0';
            integer_part /= 10;
            i--;
        }
        while (i)
        {
            data_temp[i] = ' ';
            i--;
        } //把其余地方显示空格
        LCD_TFT::replace_text(location, data_temp, is_refresh);
        while (i < char_per_line)
        {
            data_temp[i] = ' '; //字符清零
            i++;
        }
    }
}
//替换某行字符
void LCD_TFT::replace_text(u8 location, const char text[], bool is_refresh)
{
    if (location < 10 || location >= 0)
    {
        int j = 0;
        while (j < char_per_line && text[j] != '\0')
        {
            // if (text[j] != ' ')
            {
                LCD_TFT::toprintf[location][j] = text[j];
            }
            j++;
        }
        LCD_TFT::toprintf[location][j] = 'E';
        LCD_TFT::toprintf[location][j + 1] = '\0';
    }
    // printf("replaceed");
    if (is_refresh)
    {
        LCD_TFT::show_text_refresh(location);
    }
}

//全部更新
void LCD_TFT::show_text_refresh_all(void)
{

    for (int i = 1; i < 10; i++)
    {
        // char ch_temp[] = LCD_TFT::toprintf[i];
        tft180_show_string(0, i * 16, LCD_TFT::toprintf[i]);
    }
}
//单行更新
void LCD_TFT::show_text_refresh(u8 location)
{
    tft180_show_string(0, location * 16, toprintf[location]);
}
// void LCD_TFT::show_text_refresh(u8 location, char text[])
// {
//     lcd_clear(0x0fff);
//     if (text != "")
//     {
//         toprintf[location] = text;
//     }

//     tft180_show_string(0, location, toprintf[location].c_str());
// }
//固定的初始化开始的更新语句
void LCD_TFT::show_start_init_interface(void)
{
    LCD_TFT::replace_text(9, "onloading......");
    LCD_TFT::show_text_refresh_all();
}
void LCD_TFT::show_start_init_finished(void)
{
    tft180_clear(0x0fff);
    LCD_TFT::replace_text(9, "load finished", 1);
}